package com.chenjt.netty.eight_nio.fileChannel;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/22 9:47
 */
public class FileChannelLock {


    public static void main(String[] args) throws Exception {

        RandomAccessFile randomAccessFile = new RandomAccessFile("src/main/java/com/chenjt/netty/eight_nio/fileChannel/fileChannelLock.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        // 实现对文件的锁定，排他锁只能由一个程序进行写操作，读的话都可以由多个程序多文件进行read操作
        FileLock fileLock = fileChannel.lock(3, 6, true);

        System.out.println("valid:" + fileLock.isValid());
        System.out.println("lock type:" + fileLock.isShared());// 是不是共享锁

        fileLock.release();// 释放锁

        randomAccessFile.close();

    }

}
